package com.eques.demo.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.eques.database.DevList
import com.eques.demo.lock.LockSettingActivity
import com.eques.demo.anim.AnimalActivity
import com.eques.demo.R
import com.eques.demo.lock.MainLockActivity
import com.eques.icvss.core.module.user.BuddyType
import com.eques.util.Config
import com.eques.util.PermissionInterceptor
import com.eques.util.RoleUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions

class RecyleViewAdapter(var list: List<DevList> = listOf(), var ctx: Context) :
    RecyclerView.Adapter<RecyleViewAdapter.DevHoder>() {

    private val TAG: String? = RecyleViewAdapter::class.simpleName

    inner class DevHoder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tv_nick: TextView = itemView.findViewById(R.id.tv_nick)
        val tv_dev_stat: TextView = itemView.findViewById(R.id.tv_dev_stat)
        val tv_dev_share: TextView = itemView.findViewById(R.id.tv_dev_share)
        val tv_dev_update: TextView = itemView.findViewById(R.id.tv_dev_update)
        val tv_see: TextView = itemView.findViewById(R.id.tv_see)
        val iv_dev_img: ImageView = itemView.findViewById(R.id.iv_dev_img);

        var tv_set:TextView=itemView.findViewById(R.id.tv_set)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DevHoder {
        var view = LayoutInflater.from(ctx)
            .inflate(R.layout.item_dev_layout, parent, false)
        return DevHoder(view)
    }


    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: DevHoder, position: Int) {
        holder.tv_nick.text = if (list[position].nick == "") list[position].defaultNick else list[position].nick
        if (list[position].stat == 0) {
            holder.tv_dev_stat.text = "状态:离线"
            holder.tv_see.visibility = View.GONE
        } else if (list[position].stat == 2) {
            holder.tv_dev_stat.text = "状态:睡眠"
            holder.tv_see.visibility = View.GONE
        } else {
            holder.tv_dev_stat.text = "状态:在线"
            holder.tv_see.visibility = View.GONE
        }
        if (list[position].is_share == 1) {
            holder.tv_dev_share.text = "是否分享:子设备"
        } else {
            holder.tv_dev_share.text = "是否分享:主用户"
        }
        if (list[position].remoteupg == 2) {
            holder.tv_dev_update.text = "是否升级:可升级"
        } else {
            holder.tv_dev_update.text = "是否升级:最新版本"
        }

        holder.iv_dev_img.setImageResource(RoleUtils.getInstance().getRole(list[position].role))

        holder.tv_set.setOnClickListener {
            var intent = Intent(ctx, LockSettingActivity::class.java)
            intent.putExtra("devId", list[position].bid)
            intent.putExtra("devType", list[position].role)
            ctx.startActivity(intent)
        }

        holder.itemView.setOnClickListener {
            XXPermissions.with(ctx) //单个权限
                .permission(Config.PERMISSIONS_RECORD_AUDIO)
                .interceptor(PermissionInterceptor()) //  .unchecked()
                .request(OnPermissionCallback { permissions1: List<String?>?, all: Boolean ->
                    if (all) {
                        when (list[position].role) {
                            BuddyType.TYPE_WIFI_DOOR_WS2, BuddyType.TYPE_WIFI_DOOR_WS3 -> {
                                var intent = Intent(ctx, AnimalActivity::class.java)
                                intent.putExtra("devId", list[position].bid)
                                intent.putExtra("devType", list[position].role)
                                ctx.startActivity(intent)
                            }

                            BuddyType.TYPE_WIFI_DOOR_VM5,
                            BuddyType.TYPE_WIFI_DOOR_VM5_T,
                            BuddyType.TYPE_WIFI_DOOR_VL8,
                            BuddyType.TYPE_WIFI_DOOR_VL8XP,
                            BuddyType.TYPE_WIFI_DOOR_VL5_SC,
                            BuddyType.TYPE_WIFI_DOOR_VL4,
                            BuddyType.TYPE_WIFI_DOOR_VM2C,
                            BuddyType.TYPE_WIFI_DOOR_VM2CN -> {
                                var intent = Intent(ctx, MainLockActivity::class.java)
                                intent.putExtra("devId", list[position].bid)
                                ctx.startActivity(intent)
                            }
                        }

                    }
                })

        }
    }

}